# -*- coding: utf-8 -*-
from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(models.Model):
    time = models.DateTimeField(blank=True, null=True)
    username = models.CharField(unique=True, max_length=20, blank=True, null=True)
    name = models.CharField(max_length=20)
    college = models.CharField(max_length=20, blank=True, null=True)
    major = models.CharField(max_length=20, blank=True, null=True)
    phone = models.CharField(max_length=15, blank=True, null=True)
    email = models.CharField(max_length=30, blank=True, null=True)
    wechat = models.CharField(max_length=100)
    password = models.CharField(max_length=200)
    role = models.CharField(max_length=1)
    dormitory = models.CharField(max_length=10, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user'

class string_with_title(str):
    """ 用来修改admin中显示的app名称,因为admin app 名称是用 str.title()显示的,
    所以修改str类的title方法就可以实现.
    """
    def __new__(cls, value, title):
        instance = str.__new__(cls, value)
        instance._title = title
        return instance

    def title(self):
        return self._title

    __copy__ = lambda self: self
    __deepcopy__ = lambda self, memodict: self

# Create your models here.
STATUS = {
        0: u'正常',
        1: u'草稿',
        2: u'删除',
}

# 资讯来源
NEWS = {
        0: u'oschina',
        1: u'chiphell',
        2: u'freebuf',
        3: u'cnBeta',
}
IS_READ = {
        0: u'未读',
        1: u'已读'
}

class Column(models.Model):
    name = models.CharField(max_length=40)
    summary = models.TextField()
    class_id = models.CharField(max_length=100,db_column='class')
    username = models.ForeignKey(User, models.DO_NOTHING, db_column='username',to_field="username")
    create_time = models.DateTimeField()
    zy_id = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'blog_column'







class Article(models.Model):
    id= models.IntegerField()
    username = models.ForeignKey(User, verbose_name=u'作者',to_field="username",db_column='username')
    category = models.CharField(max_length=20)
    title = models.CharField(max_length=100, verbose_name=u'标题')
    
    img = models.CharField(max_length=200,
                           default='/static/img/article/default.jpg')
    tags = models.CharField(max_length=200, null=True, blank=True,
                            verbose_name=u'标签', help_text=u'用逗号分隔')
    summary = models.TextField(verbose_name=u'摘要')
    content = models.TextField(verbose_name=u'正文')
    view_times = models.IntegerField(default=0)
    zan_times = models.IntegerField(default=0)

    is_top = models.BooleanField(default=0, verbose_name=u'置顶')
    rank = models.IntegerField(default=0, verbose_name=u'排序')
    status = models.IntegerField(default=0, choices=STATUS.items(),
                                 verbose_name='状态')
    pub_time = models.DateTimeField(default=False, verbose_name=u'发布时间')
    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
    update_time = models.DateTimeField(u'更新时间', auto_now=True)
    column = models.ForeignKey(Column, models.DO_NOTHING, db_column='column',to_field="id")

    def get_tags(self):
        tags_list = self.tags.split(',')
        while '' in tags_list:
            tags_list.remove('')

        return tags_list

    class Meta:
        verbose_name_plural = verbose_name = u'文章'
        ordering = ['rank', '-is_top', '-pub_time', '-create_time']
        app_label = string_with_title('blog', u"博客管理")

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('article-detail-view', args=(self.id))

    def __unicode__(self):
            return self.title

    __str__ = __unicode__







class News(models.Model):
    title = models.CharField(max_length=100, verbose_name=u'标题')
    summary = models.TextField(verbose_name=u'摘要')
    news_from = models.IntegerField(default=0, choices=NEWS.items(),
                                    verbose_name='来源')
    url = models.CharField(max_length=200, verbose_name=u'源地址')
    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
    pub_time = models.DateTimeField(default=False, verbose_name=u'发布时间')

    class Meta:
        verbose_name_plural = verbose_name = u'资讯'
        ordering = ['-title']
        app_label = string_with_title('blog', u"博客管理")

    def get_absolute_url(self):
        from django.core.urlresolvers import reverse
        return reverse('news-detail-view', args=(self.pk,))

    def __unicode__(self):
        return self.title

    __str__ = __unicode__




class Comment(models.Model):
    username = models.ForeignKey(User, verbose_name=u'用户',to_field="username",db_column='username')
    article = models.ForeignKey(Article, verbose_name=u'文章')
    text = models.TextField(verbose_name=u'评论内容')
    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)

    parent = models.ForeignKey('self', default=None, blank=True, null=True,
                               verbose_name=u'引用')

    class Meta:
        verbose_name_plural = verbose_name = u'评论'
        ordering = ['-create_time']
        app_label = string_with_title('vmaig_comments', u"评论管理")

    def __unicode__(self):
        return self.article.title + '_' + str(self.pk)

    __str__ = __unicode__





class Notification(models.Model):
    title = models.CharField(max_length=100, verbose_name=u'标题')
    text = models.TextField(verbose_name=u'内容')
    url = models.CharField(max_length=200, verbose_name=u'连接',
                           null=True, blank=True)
    from_user = models.ForeignKey(User,
                                  default=None, blank=True, null=True,
                                  related_name='from_user_notification_set',
                                  verbose_name=u'发送者',to_field="username")
    to_user = models.ForeignKey(User,
                                related_name='to_user_notification_set',
                                verbose_name=u'接收者',to_field="username")
    type = models.CharField(max_length=20, verbose_name=u'类型',
                            null=True, blank=True)

    is_read = models.IntegerField(default=0, choices=IS_READ.items(),
                                  verbose_name=u'是否读过')

    create_time = models.DateTimeField(u'创建时间', auto_now_add=True)
    update_time = models.DateTimeField(u'更新时间', auto_now=True)

    class Meta:
        verbose_name_plural = verbose_name = u'消息'
        ordering = ['-create_time']
        db_table = 'vmaig_system_notification'


